﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Teacher)]
    public class AdminSupportController : BaseController
    {

        [UploadAuth]
        [UrlStatFilter]
        public ActionResult Guide()
        {
            var info = CourseSettingBLL.GetFromDb();
            if (info == null)
                return NotFound();
            return View(info);
        }

        [HttpPost]
        [ValidateInput(false)]
        [UploadedAuth]
        public ActionResult Guide(CourseSettingInfo info)
        {
            var oldInfo = CourseSettingBLL.GetFromDb();
            if (oldInfo.CourseGuideInfo == null)
            {
                var addCommon = new CommonContentInfo { DetailContent = info.CourseGuideInfo.DetailContent ?? "" };
                if (!CommonContentBLL.Add(addCommon))
                {
                    SetAlertMsg("未知错误.");
                    return View();
                }
                oldInfo.CourseGuideId = addCommon.Id;
                if (CourseSettingBLL.Edit(oldInfo))
                {
                    AttachmentsHelper.ClearUselessAttach(addCommon.DetailContent, ViewData["ranStr"].ToString(), addCommon.Id);
                    SetAlertMsg("编辑成功.");
                    return View(oldInfo);
                }
                SetAlertMsg("未知错误.");
                return View();
            }
            oldInfo.CourseGuideInfo.DetailContent = info.CourseGuideInfo.DetailContent ?? "";
            if (CommonContentBLL.Edit(oldInfo.CourseGuideInfo))
            {
                AttachmentsHelper.ClearUselessAttach(oldInfo.CourseGuideInfo.DetailContent, ViewData["ranStr"].ToString(), oldInfo.CourseGuideInfo.Id);
                SetAlertMsg("编辑成功.");
                return View(oldInfo);
            }
            SetAlertMsg("未知错误.");
            return View();
        }

        [UploadAuth]
        [UrlStatFilter]
        public ActionResult Helper()
        {
            var info = CourseSettingBLL.GetFromDb();
            if (info == null)
                return NotFound();
            return View(info);
        }

        [HttpPost]
        [ValidateInput(false)]
        [UploadedAuth]
        public ActionResult Helper(CourseSettingInfo info)
        {
            var oldInfo = CourseSettingBLL.GetFromDb();
            if (oldInfo.CourseHelpInfo == null)
            {
                var addCommon = new CommonContentInfo { DetailContent = info.CourseHelpInfo.DetailContent ?? "" };
                if (!CommonContentBLL.Add(addCommon))
                {
                    SetAlertMsg("未知错误.");
                    return View();
                }
                oldInfo.CourseHelperId = addCommon.Id;
                if (CourseSettingBLL.Edit(oldInfo))
                {
                    AttachmentsHelper.ClearUselessAttach(addCommon.DetailContent, ViewData["ranStr"].ToString(), addCommon.Id);
                    SetAlertMsg("编辑成功.");
                    return View(oldInfo);
                }
                SetAlertMsg("未知错误.");
                return View();
            }
            oldInfo.CourseHelpInfo.DetailContent = info.CourseHelpInfo.DetailContent ?? "";
            if (CommonContentBLL.Edit(oldInfo.CourseHelpInfo))
            {
                AttachmentsHelper.ClearUselessAttach(oldInfo.CourseHelpInfo.DetailContent, ViewData["ranStr"].ToString(), oldInfo.CourseHelpInfo.Id);
                SetAlertMsg("编辑成功.");
                return View(oldInfo);
            }
            SetAlertMsg("未知错误.");
            return View();
        }
        [UrlStatFilter]
        public ActionResult Process()
        {
            ViewData["listName"] = "ProcessListUc";
            return View();
        }

        public ActionResult ProcessListUc(int? pid, int? collegeIdSearch, int? departIdSearch, int? subjectIdSearch, int? yearIdSearch, int? classIdSearch, string uidSearch, string nameSearch)
        {
            CommonRequest.ClearCache();
            var list = LearnProcessBLL.GetList(collegeIdSearch.CheckInt(0), departIdSearch.CheckInt(0), subjectIdSearch.CheckInt(0),
               yearIdSearch.CheckInt(0), classIdSearch.CheckInt(0), uidSearch, nameSearch).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult ProcessViewUc(int id)
        {
            var info = LearnProcessBLL.Get(id);
            if (info == null)
                return NotFound();
            return PartialView(info);
        }

        public ActionResult SoftwareEdit(int? id)
        {
            if (id.HasValue)
            {
                var info = SoftwareBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
                return View(info);
            }
            return View(new SoftwareInfo());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SoftwareEdit(SoftwareInfo info)
        {
            if (info.Id != 0)
            {
                var oldInfo = SoftwareBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                info.FileSrc = oldInfo.FileSrc;
            }
            ViewData.Model = info;

            if (!ModelState.IsValid)
                return View();
            info.SoftName = info.SoftName ?? "";
            var msg = "";
            var fileName = ResourcesHelper.UploadSoftware(Request.Files["UpFile"], out msg, info.Id == 0);
            if (!string.IsNullOrEmpty(msg))
            {
                SetAlertMsg(msg);
                return View();
            }
            info.FileSrc = fileName;
            if (info.Id != 0)
            {
                var oldInfo = SoftwareBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                var needDelFile = "";
                if (!string.IsNullOrEmpty(info.FileSrc))
                {
                    needDelFile = oldInfo.FileSrc;
                    oldInfo.FileSrc = info.FileSrc;
                }
                oldInfo.UpdateTime = DateTime.Now;
                oldInfo.SoftName = info.SoftName;
                if (SoftwareBLL.Edit(oldInfo))
                {
                    if (!string.IsNullOrEmpty(needDelFile))
                        System.IO.File.Delete(Server.MapPath("~/") + needDelFile);
                    SetAlertMsg("编辑成功.");
                    ViewData.Model = oldInfo;
                    return View();
                }
                SetAlertMsg("未知错误.");
                return View();
            }
            if (SoftwareBLL.Add(info))
            {
                SetAlertMsg("添加成功.");
                return View();
            }
            SetAlertMsg("未知错误.");
            return View();
        }
        [UrlStatFilter]
        public ActionResult Software()
        {
            return View();
        }

        public ActionResult SoftwareListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = SoftwareBLL.GetList().OrderByDescending(o => o.UpdateTime);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult SoftwareDel(int id)
        {
            var info = SoftwareBLL.Get(id);
            if (info == null)
                return NotFound();
            if (SoftwareBLL.Del(id))
            {
                ResourcesHelper.DeleteFile(info.FileSrc);
                SetAlertMsg("删除成功.", true);
            }
            return RedirectToAction("Software");
        }


    }
}
